package com.douguo.recipe;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.douguo.recipe.network.Constant;
import com.douguo.recipe.network.GetPhoto;
import com.douguo.recipe.network.GetRecipe;
import com.douguo.recipe.network.Util;
import com.douguo.recipe.object.Caipu;
import com.douguo.recipe.object.Step;
import com.douguo.recipe.provider.DBHelper;
import com.douguo.recipe.sina.Cache;
import com.douguo.recipe.sina.Weibo;
import com.douguo.recipe.view.NavigationView;

public class RecipeActivity extends DouguoActivity {
	
	private static final String TAG = "RecipeActivity";

	// --------------data------------
	// private String mRecipeId;
	private int[] mIds;
	private int mCurrentIdPos;
	private List<Map<String, String>> mStepsList;
	private Caipu mCaipu;
	// ------------------------------
	// --------------ui--------------
//	private ViewGroup mVgOverview;
//	private ViewGroup mVgIngredient;
//	private ViewGroup mVgStep;
//	private ListView mLvSteps;
	private ImageView mIvPhoto;
	private Button mIbPrev;
	private Button mIbNext;
	private Button mAdd;
	private ImageButton mIbShowLargeImage;
	private RelativeLayout mRlLargeImage;
	private ImageView mIvLargePhoto;
	private LinearLayout mLyTips;
	private TextView mTvTips;
	private Button mBtnCaixi;
	private Button mBtnCaipin;
	private Button mBtnKouwei;
	private Button mBtnTese;
	private NavigationView mNavigation;
	private Button mBtnBack;
	private LinearLayout mListLayout;
	// ------------------------------
	// --------------adapter----------
	private SimpleAdapter mStepsAdapter;
	// -------------------------------
	// ------------DBHelper-----------
	private DBHelper mDBHelper;
	// -------------------------------

	private Calendar mCalendar;

	private static final int DIALOG_RETRY = 0;
	private static final int DIALOG_WAIT = 1;
	private static final int DIALOG_ADD = 2;
	private static final int DIALOG_DATE_PICK = 3;
	private static final int DIALOG_CAILI_OVERRIDE = 4;
	
	private static final int MENU_HOME = Menu.FIRST;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			boolean dbFlag = false;
			switch (msg.what) {
			case Constant.GET_RECIPE_DATABASE:
				dbFlag = true;
			case Constant.GET_RECIPE_NETWORK:
				final Caipu caipu = (Caipu) msg.obj;

				if (caipu != null) {
					final Intent intent = new Intent(RecipeActivity.this,
							RecipelistActivity.class);
					final Bundle bundle = new Bundle();

					TextView tvTitle = (TextView) findViewById(R.id.text_dishName);
					tvTitle.setText(caipu.name);
					TextView tvAuthor = (TextView) findViewById(R.id.text_dishAuthor);
					tvAuthor.setText(tvAuthor.getText()+" "+caipu.author);

					Button btnCaixi = (Button) findViewById(R.id.text_dishCaixi);
					final String caixiName = mDBHelper.getSortName(caipu.caixi);
					if (caixiName != null) {
						btnCaixi.setText(caixiName);
						btnCaixi.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								bundle.putStringArray("ids",
										new String[] { caipu.caixi.id });
								bundle.putIntArray("types",
										new int[] { Constant.TYPE_CAIXI });
								bundle.putString("title", caixiName);
								intent.putExtras(bundle);
								
								startActivity(intent);
							}
						});
					}

					Button btnCaipin = (Button) findViewById(R.id.text_dishCaipin);
					final String caipinName = mDBHelper.getSortName(caipu.caipin);
					if (caipinName != null) {
						btnCaipin.setText(caipinName);
						btnCaipin.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								bundle.putStringArray("ids",
										new String[] { caipu.caipin.id });
								bundle.putIntArray("types",
										new int[] { Constant.TYPE_CAIPIN });
								bundle.putString("title", caipinName);
								intent.putExtras(bundle);
								startActivity(intent);
							}
						});
					}

					Button btnKouwei = (Button) findViewById(R.id.text_dishKouwei);
					final String kouweiName = mDBHelper.getSortName(caipu.kouwei);
					if (kouweiName != null) {
						btnKouwei.setText(kouweiName);
						btnKouwei.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								bundle.putStringArray("ids",
										new String[] { caipu.kouwei.id });
								bundle.putIntArray("types",
										new int[] { Constant.TYPE_KOUWEI });
								bundle.putString("title", kouweiName);
								intent.putExtras(bundle);
								startActivity(intent);
							}
						});
					}

					Button btnTese = (Button) findViewById(R.id.text_dishTese);
					final String teseName = mDBHelper.getSortName(caipu.tese);
					if (teseName != null) {
						btnTese.setText(teseName);
						btnTese.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								bundle.putStringArray("ids",
										new String[] { caipu.tese.id });
								bundle.putIntArray("types",
										new int[] { Constant.TYPE_TESE });
								bundle.putString("title", teseName);
								intent.putExtras(bundle);
								startActivity(intent);
							}
						});
					}

					TextView tvNandu = (TextView) findViewById(R.id.text_dishNandu);
					String nanduName = mDBHelper.getSortName(caipu.nandu);
					if (nanduName != null)
						tvNandu.setText(nanduName);

					TextView tvTime = (TextView) findViewById(R.id.text_proTime);
					tvTime.setText(caipu.time);

					TextView tvMajor = (TextView) findViewById(R.id.majorMaterial);
					tvMajor.setText(caipu.major);

					TextView tvMinor = (TextView) findViewById(R.id.otherMaterial);
					tvMinor.setText(caipu.minor);

					int i = 1;
					if (caipu.steps != null) {

						for (Step step : caipu.steps) {
							
							Map<String, String> item = new HashMap<String, String>();
//							item.put("number", String.valueOf(i++));
							item.put("img", step.imgPath);
							item.put("content", step.content);
							mStepsList.add(item);
							View view = LayoutInflater.from(RecipeActivity.this).inflate(R.layout.irecipe_steps,null);
							if(i == caipu.steps.size()) {
								view.findViewById(R.id.image_recipeStepDivider).setVisibility(View.GONE);
							}
							
							TextView tvNumber = (TextView) view.findViewById(R.id.text_recipeStepNum);
							tvNumber.setText(String.valueOf(i++) + ".");
							TextView tvContent = (TextView) view.findViewById(R.id.text_recipeStepDet);
							tvContent.setText("        "+step.content);
							mListLayout.addView(view);
						}
					}

					if (caipu.tips != null && caipu.tips.length() > 0) {
//						View view = LayoutInflater.from(RecipeActivity.this)
//								.inflate(R.layout.step_listitem_footer, null);
//						view.setVisibility(View.VISIBLE);
//						TextView tvTips = (TextView) view
//								.findViewById(R.id.tips);
//						tvTips.setText(caipu.tips);
						mLyTips.setVisibility(View.VISIBLE);
						mTvTips.setText(caipu.tips);
//						mLvSteps.addFooterView(view, null, false);

					}

//					mLvSteps.setAdapter(mStepsAdapter);
//					mIvPhoto.setImageResource(R.drawable.cookdefault140);
					
					//缩略图
					if (caipu.thumbPath == null || caipu.thumbPath.length() == 0) {
						Log.d(TAG,"thumbPath + null");
						mIvPhoto.setImageResource(R.drawable.cookdefault80);
					} else if (caipu.thumbPath.contains("/sdcard")) {
						Log.d(TAG,caipu.thumbPath);
						Bitmap bitmap = Util.readBitmapFromSDCard(caipu.thumbPath);
						if (bitmap != null) {
							mIvPhoto.setImageBitmap(bitmap);
						}
					
					} else if (Util.isUseNetworkPreferences(RecipeActivity.this)
							&& Util.isDownloadPhotoPreferences(RecipeActivity.this)) {
						String thumhnailPath = caipu.thumbPath.replaceFirst("300_","80_");
						mIvPhoto.setImageResource(R.drawable.cookdefault140_downloading);
						GetPhoto.getPhoto(0, thumhnailPath, mHandler);
						Log.d(TAG,caipu.thumbPath);
					} else {
						Log.d(TAG,caipu.thumbPath);
////						mIvPhoto.setImageResource(R.drawable.cookdefault140_clickdown);
////						mIvPhoto.setOnClickListener(new OnClickListener() {
////							public void onClick(View v) {
////								mIvPhoto.setImageResource(R.drawable.cookdefault140_downloading);
////								GetPhoto.getPhoto(0, mCaipu.picPath, mHandler);
////							}
//
////						});
					}
					
					//大图
					if (caipu.photoPath == null || caipu.photoPath.length() == 0) {
						mIvLargePhoto.setImageResource(R.drawable.cookdefault140);
					} else if (caipu.photoPath.contains("/sdcard")) {
						Bitmap bitmap = Util.readBitmapFromSDCard(caipu.photoPath);
						if (bitmap != null) {
							mIvLargePhoto.setImageBitmap(bitmap);
						}
					
					} else if (Util.isUseNetworkPreferences(RecipeActivity.this)
							&& Util.isDownloadPhotoPreferences(RecipeActivity.this)) {
						mIvLargePhoto.setImageResource(R.drawable.cookdefault140_downloading);
//						GetPhoto.getPhoto(1, caipu.photoPath, mHandler);
					} else {
////						mIvPhoto.setImageResource(R.drawable.cookdefault140_clickdown);
////						mIvPhoto.setOnClickListener(new OnClickListener() {
////							public void onClick(View v) {
////								mIvPhoto.setImageResource(R.drawable.cookdefault140_downloading);
////								GetPhoto.getPhoto(0, mCaipu.picPath, mHandler);
////							}
//
////						});
					}
					
					
					mStepsAdapter.notifyDataSetChanged();
					if (dbFlag == false && !isFinishing()) {
						mDBHelper.addRecipe(caipu);
						try {
							dismissDialog(DIALOG_WAIT);
						} catch (IllegalArgumentException e) {
							// do nothing
						}
					}
					mCaipu = caipu;

				} else {
					if (!isFinishing()) {
						showDialog(DIALOG_RETRY);
					}
				}
				break;
			case Constant.GET_PHOTO:
				
				SoftReference<Bitmap> refBitmap = (SoftReference<Bitmap>) msg.obj;
				boolean flag = false;
				if (refBitmap != null) {
					Bitmap bitmap = refBitmap.get();
					if (bitmap != null) {
						String filename = Util.saveBitmapToSDCard(bitmap,false);
						if(msg.arg1 == 0){//小图
							mIvPhoto.setImageBitmap(bitmap);
	//						mIvPhoto.setOnClickListener(null);
							
							if (filename != null) {
								// TODO update recipe db
								mCaipu.thumbPath = filename;
								mDBHelper.updateRecipeThumb(mCaipu);
							}
						}
						else if(msg.arg1 == 1) {//大图
							mIvLargePhoto.setImageBitmap(bitmap);
							mCaipu.photoPath = filename;
							mDBHelper.updateRecipePhoto(mCaipu);
						}
						flag = true;
						
					} else {
						flag = false;
					}
				} else {
					flag = false;
				}
				if (!flag) {
//					mIvPhoto.setImageResource(R.drawable.cookdefault140_clickdown);// TODO
					// change
					// the photo
//					mIvPhoto.setOnClickListener(new OnClickListener() {
//						public void onClick(View v) {
//							mIvPhoto.setImageResource(R.drawable.cookdefault140_downloading);// TODO
							// change
							// the
							// photo
//							GetPhoto.getPhoto(0, mCaipu.picPath, mHandler);
//						}

//					});
				}
				

				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.recipe);
		mDBHelper = new DBHelper(this);
		mStepsList = new ArrayList<Map<String, String>>();
		mStepsAdapter = new SimpleAdapter(this, mStepsList,
				R.layout.step_listitem, new String[] { "number", /* "img", */
				"content" }, new int[] { R.id.stepNumber, /* R.id.stepImg, */
				R.id.stepContent });
		initBundle();
		initUI();
		initData();
	}

	private void initData() {
		Caipu caipu = mDBHelper.getRecipe(mCaipu.id);
		if (caipu == null) {
			GetRecipe.getCaipu(mCaipu.id, mHandler);
//			GetPhoto.getPhoto(0, mCaipu.picPath, mHandler);
			showDialog(DIALOG_WAIT);
		} else {
			mHandler.sendMessage(mHandler.obtainMessage(
					Constant.GET_RECIPE_DATABASE, caipu));
		}
	}

	private void initBundle() {
		Intent intent = getIntent();
		if (intent != null) {
			Bundle bundle;
			bundle = intent.getExtras();
			if (bundle == null){
				bundle = (Bundle)Cache.remove("Recipe");
			}
			mCaipu = new Caipu();
			mCaipu.id = bundle.getString("recipeId");
			mIds = bundle.getIntArray("ids");
			mCurrentIdPos = bundle.getInt("pos");
		} else {
			finish();
		}
	}

	private void initUI() {
//		mVgOverview = (ViewGroup) findViewById(R.id.irecipe_overview);
//		mVgIngredient = (ViewGroup) findViewById(R.id.irecipe_ingredient);
//		mVgStep = (ViewGroup) findViewById(R.id.irecipe_step);
//		final Button overview = (Button) findViewById(R.id.button_dishProduct);
//		final Button material = (Button) findViewById(R.id.button_dishMaterial);
//		final Button step = (Button) findViewById(R.id.button_dishDetailStep);
		mLyTips = (LinearLayout) findViewById(R.id.content_main_tips_layout);
		mTvTips = (TextView) findViewById(R.id.content_main_tips_text);
		
		mAdd = (Button) findViewById(R.id.imageButton_favor);
		mAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//收藏此菜，分享到新浪微博
				String[] menuItem = { "收藏此菜", "分享到新浪微博" };
				new AlertDialog.Builder(RecipeActivity.this)
						.setTitle("收藏&分享")
						.setItems(menuItem, new AlertDialog.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (which == 0) {
									save();
								} else {
									weibo();
								}
							}
							
						}).show();
			}
		});
		mIvLargePhoto = (ImageView) findViewById(R.id.recipe_large_image);
		mIbShowLargeImage = (ImageButton) findViewById(R.id.imageButton_dishPlus);
		mRlLargeImage = (RelativeLayout) findViewById(R.id.recipe_mask_layout);
		mRlLargeImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mRlLargeImage.setVisibility(View.GONE);
				
			}
		});
		mIbShowLargeImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
//				if(!mIsLargePhotoLoaded) {
				openLargePhoto();
			}
		});
		mIvPhoto = (ImageView) findViewById(R.id.image_dishHead);
		mIvPhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				openLargePhoto();
			}
		});
//		mIvPhoto.setImageBitmap(mThumbnailBitmap);
		mIbPrev = (Button) findViewById(R.id.button_recipe_prev);
		mIbNext = (Button) findViewById(R.id.button_recipe_next);

		if (mCurrentIdPos <= 0) {
			mIbPrev.setVisibility(View.INVISIBLE);
		}

		if (mCurrentIdPos >= mIds.length - 1) {
			mIbNext.setVisibility(View.INVISIBLE);
		}

		mIbPrev.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startActivity(mCurrentIdPos - 1);

			}
		});
		mIbNext.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startActivity(mCurrentIdPos + 1);
			}
		});
//		mAdd.setOnClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				showDialog(DIALOG_ADD);
//			}
//		});
		mListLayout = (LinearLayout) findViewById(R.id.content_main_dishDetail);
		mNavigation = new NavigationView(this,NavigationView.STATE_NONE);
		mBtnBack = (Button) findViewById(R.id.imageButton_back);
		mBtnBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
		
		
	}
	
	private void save(){
		if (mDBHelper.addFavor(mCaipu)) {
			Util.showShortToast(
					RecipeActivity.this,
					R.string.addSuccessfully);
		} else {
			Util.showShortToast(
					RecipeActivity.this,
					R.string.addToFavorFail);
		}
	}
	
	private void weibo(){
		Cache.put("Recipe", getIntent().getExtras());
		
		//http://www.douguo.com/cookbook/8157.html
		//#豆果美食#“什锦土豆泥沙拉”原来这么做的啊：   http://t.cn/heemrk  （分享自 @豆果网），快来尝尝！
		// http://t.cn/heeujH http://www.douguo.com/cookbook/8156.html
		
		//#美食#我刚才看到这道菜，非常喜欢分享给大家。菜名  http://*******（来自豆果美食Android版）
		
		StringBuilder sb = new StringBuilder();
		sb.append("#豆果美食#");
		sb.append("我刚才看到这道菜，非常喜欢分享给大家。");
		sb.append("“"+ mCaipu.name +"”");
//		sb.append("原来这么做的啊：");
		
		sb.append("http://www.douguo.com/cookbook/");
		sb.append(mCaipu.id);
		sb.append(".html");
		
		sb.append("快来尝尝！");

		mIvPhoto.buildDrawingCache();
		Weibo.updateStatus(RecipeActivity.this, sb.toString(), mIvPhoto.getDrawingCache());
		
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_RETRY:
			return new AlertDialog.Builder(RecipeActivity.this)
					.setMessage(R.string.getRecipeListFail)
					.setPositiveButton(R.string.retry,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									GetRecipe.getCaipu(mCaipu.id, mHandler);
									showDialog(DIALOG_WAIT);
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									try {
										dismissDialog(DIALOG_RETRY);
										dismissDialog(DIALOG_WAIT);
									} catch (IllegalArgumentException e) {
										// do nothing
									}
									finish();
								}
							}).setOnKeyListener(new OnKeyListener() {

						public boolean onKey(DialogInterface dialog,
								int keyCode, KeyEvent event) {
							if (keyCode == KeyEvent.KEYCODE_BACK) {
								try {
									dismissDialog(DIALOG_RETRY);
									dismissDialog(DIALOG_WAIT);
								} catch (IllegalArgumentException e) {
									// do nothing
								}
								finish();
								return true;
							}
							return false;
						}
					}).create();
		case DIALOG_WAIT:
			ProgressDialog progressDialog = new ProgressDialog(
					RecipeActivity.this);
			progressDialog.setMessage(RecipeActivity.this
					.getString(R.string.recipeDownloading));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setOnKeyListener(new OnKeyListener() {

				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_BACK) {
						try {
							dismissDialog(DIALOG_WAIT);
						} catch (IllegalArgumentException e) {
							// do nothing
						}
						finish();
						return true;
					}
					return false;
				}
			});
			return progressDialog;
		case DIALOG_ADD:
			return new AlertDialog.Builder(RecipeActivity.this)
					.setNegativeButton(R.string.cancel, null)
					.setItems(R.array.addRecipe,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									if (which == 0) {
										if (mDBHelper.addFavor(mCaipu)) {
											Util.showShortToast(
													RecipeActivity.this,
													R.string.addSuccessfully);
										} else {
											Util.showShortToast(
													RecipeActivity.this,
													R.string.addToFavorFail);
										}
									} else if (which == 1) {
										showDialog(DIALOG_DATE_PICK);

									}
								}
							}).create();
		case DIALOG_DATE_PICK:
			mCalendar = Calendar.getInstance();
			mCalendar.setTimeInMillis(System.currentTimeMillis());
			int year = mCalendar.get(Calendar.YEAR);
			int month = mCalendar.get(Calendar.MONTH);
			int day = mCalendar.get(Calendar.DATE);
			return new DatePickerDialog(this,
					new DatePickerDialog.OnDateSetListener() {

						public void onDateSet(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {
							if (mDBHelper.addCalendar(mCaipu, year,
									monthOfYear + 1, dayOfMonth)) {
								Util.showShortToast(RecipeActivity.this,
										R.string.addSuccessfully);
							} else {
								mCalendar.set(year, monthOfYear, dayOfMonth);
								showDialog(DIALOG_CAILI_OVERRIDE);
							}

						}

					}, year, month, day);
		case DIALOG_CAILI_OVERRIDE:
			return new AlertDialog.Builder(this)
					.setPositiveButton(R.string.confirm,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									int year = mCalendar.get(Calendar.YEAR);
									int month = mCalendar.get(Calendar.MONTH);
									int day = mCalendar.get(Calendar.DATE);
									if (mDBHelper.updateCalendar(mCaipu, year,
											month + 1, day)) {
										Util.showShortToast(
												RecipeActivity.this,
												R.string.addSuccessfully);
									}
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									// do nothing

								}
							}).setMessage(R.string.addToCalendarAsk).create();
		}
		return null;
	}

	private void startActivity(int pos) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		intent.setClass(this, RecipeActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("recipeId", String.valueOf(mIds[pos]));
		bundle.putInt("pos", pos);
		bundle.putIntArray("ids", mIds);
		intent.putExtras(bundle);
		startActivity(intent);
	}
	
	private void openLargePhoto() {
		if(Util.isUseNetworkPreferences(RecipeActivity.this) ||( mCaipu.photoPath != null && mCaipu.photoPath.contains("sdcard"))) {
			mRlLargeImage.setVisibility(View.VISIBLE);
			GetPhoto.getPhoto(1, mCaipu.photoPath, mHandler);
		}
		else {
			//提示已关闭网络
			Toast.makeText(RecipeActivity.this, R.string.networkClose, Toast.LENGTH_LONG).show();
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	if(mRlLargeImage.getVisibility() == View.GONE) {
        		finish();
        	}else {
        		mRlLargeImage.setVisibility(View.GONE);
        	}
           return true;
        }
        return false;
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_HOME, 0, R.string.backToHome);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_HOME:
			Intent homeIntent = new Intent(this, HomeActivity.class);
			homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(homeIntent);
			break;
		}
		return true;

	}

}
